import tensorflow as tf
import pandas as pd

tf.logging.set_verbosity(tf.logging.INFO)


CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth',
                    'PetalLength', 'PetalWidth', 'Species']

SPECIES = ['Setosa', 'Versicolor', 'Virginica']


batch_size = 100
train_steps = 1000


train_path = tf.keras.utils.get_file("iris_training.csv", "")
test_path = tf.keras.utils.get_file("iris_test.csv", "")


CSV_TYPES = [[0.0], [0.0], [0.0], [0.0], [0]]

def load_data():
    y_name= 'Species'


    train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
    train_x, train_y = train, train.pop(y_name)
    
    test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
    test_x, test_y = test, test.pop(y_name)
    return (train_x, train_y), (test_x, test_y)

(train_x, train_y), (test_x, test_y) = load_data()

my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))


classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns,
    hidden_units=[10, 10],
    n_classes=3)

def train_input_fn(features, labels, batch_size):
    # convert data to tf.data.Dataset
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)
    return dataset


def eval_input_fn(features, labels, batch_size):
    """An input function for evaluation or prediction"""
    features=dict(features)
    if labels is None:
        # No labels, use only features.
        inputs = features
    else:
        inputs = (features, labels)

    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices(inputs)

    # Batch the examples
    assert batch_size is not None, "batch_size must not be None"
    dataset = dataset.batch(batch_size)

    # Return the dataset.
    return dataset

def _parse_line(line):
    # Decode the line into its fields
    fields = tf.decode_csv(line, record_defaults=CSV_TYPES)

    # Pack the result into a dictionary
    features = dict(zip(CSV_COLUMN_NAMES, fields))

    # Separate the label from the features
    label = features.pop('Species')

    return features, label


def csv_input_fn(csv_path, batch_size):
    # Create a dataset containing the text lines.
    dataset = tf.data.TextLineDataset(csv_path).skip(1)

    # Parse each line.
    dataset = dataset.map(_parse_line)

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)

    # Return the dataset.
    return dataset
#classifier.train(
#    input_fn = lambda:train_input_fn(train_x, train_y, batch_size),
#    steps=train_steps)
#
#eval_result = classifier.evaluate(
#    input_fn=lambda: eval_input_fn(test_x, test_y,
#                                                batch_size))



classifier.train(
    input_fn = lambda:csv_input_fn(train_path, batch_size),
    steps=train_steps)

eval_result = classifier.evaluate(
    input_fn=lambda: csv_input_fn(test_path, batch_size))


if __name__ == "__main__":
    load_data()
